package cc.admin.modules.sys.service.impl;

import cc.admin.common.util.DySmsEnum;
import cc.admin.common.util.DySmsHelper;
import cc.admin.modules.sys.entity.MessageNotification;
import cc.admin.modules.sys.entity.ShortMessages;
import cc.admin.modules.sys.entity.ShortMessagesError;
import cc.admin.modules.sys.entity.SysUser;
import cc.admin.modules.sys.mapper.MessageNotificationMapper;
import cc.admin.modules.sys.mapper.ShortMessagesMapper;
import cc.admin.modules.sys.mapper.SysUserMapper;
import cc.admin.modules.sys.service.IShortMessagesService;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description:
 * @Author: cc-admin
 * @Date: 2023-03-01
 * @Version: V1.0.0
 */
@Service
public class ShortMessagesServiceImpl extends ServiceImpl<ShortMessagesMapper, ShortMessages> implements IShortMessagesService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private MessageNotificationMapper messageNotificationMapper;
    @Autowired
    private ShortMessagesMapper shortMessagesMapper;

    @Override
    public boolean sendTextMessage(String phone, JSONObject templateParamJson, DySmsEnum dySmsEnum) {
        boolean result = true;
        SysUser sysUser = sysUserMapper.getUserByPhone(phone);
        if(sysUser !=null){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id", "sms");
            MessageNotification messageNotification = messageNotificationMapper.selectOne(queryWrapper);
            if (messageNotification != null && "1".equals(messageNotification.getShortMessage())) {
                if ("2".equals(messageNotification.getShortScope()) || sysUser.getUserFlag().equals(messageNotification.getShortScope())) {
                    ShortMessages shortMessages = new ShortMessages();
                    try {
                        shortMessages.setPhone(phone);
                        shortMessages.setTemplateCode(dySmsEnum.getTemplateCode());
                        shortMessages.setTemplateParam(dySmsEnum.getKeys());
                        shortMessages.setUserId(sysUser.getId());
                        shortMessages.setUserName(sysUser.getRealname());
                        shortMessages.setOperatorTime(new Date());
                        shortMessagesMapper.insert(shortMessages);
                        result = DySmsHelper.sendSms(phone, templateParamJson, dySmsEnum);
                    } catch (ClientException e) {
                        ShortMessagesError shortMessagesError = new ShortMessagesError();
                        shortMessagesError.setPhone(phone).setTemplatCode(dySmsEnum.getTemplateCode()).setTemplateName(dySmsEnum.getKeys())
                                .setUserId(sysUser.getId()).setUserName(sysUser.getRealname()).setOperatorTime(new Date()).setCauseOfFailure(e.getMessage());
                        e.printStackTrace();
                    }
                }
            }
        }
        return result;
    }
}
